//获取应用实例
const app = getApp()
let request = require('../../utils/request.js')

Page({
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    pageHeight: app.globalData.pageHeight,
    paddingTop: app.globalData.paddingTop,
    changeParkingDialog: false,
    handrailDialog: false,
    parkInfo: {},
    parkingOwnerData: {},
    parkList: [],
    parkName: '',
    parkId: '',
    parkSource: '',
    aisleList: [],
    selectAisleData: {},
    todayIncomeFromShop: 0
  },
  onLoad: function () {
    let token = wx.getStorageSync('token')
    let parkId = wx.getStorageSync('parkId') ? wx.getStorageSync('parkId') : ''
    let parkSource = wx.getStorageSync('parkInfo') ? wx.getStorageSync('parkInfo').parkSource : ''
    this.setData({ parkId, parkSource })
    if (!token || token == '') {
      wx.redirectTo({ url: '/pages/login/login' })
      return
    }
  },
  onShow() {
    this.setData({
      parkId: wx.getStorageSync('parkId') ? wx.getStorageSync('parkId') : ''
    })
    console.log()
    //  获取管理员停车场信息
    this.getManagerParkInfo()
    //  获取停车场主信息
    this.getParkingOwnerData(this.data.parkId)
  },
  /**
   * 今日收益
   */
  getToadyMoney(parkId) {
    let url = '/order/orderInfo/getParkTodayRevenue'
    let parmas = {
      parkId: this.data.parkId ? this.data.parkId : parkId
    }
    request.http(url, 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          this.setData({ 'parkInfo.todayMoney': data.data })
        }
      })
  },
  /**
   * 额外收入
   */
  getParkTodayExtraRevenue(){
    request.http('/order/orderInfo/getParkExtraRevenue', 'GET', { parkId: this.data.parkId, dateType: 1 })
      .then(res => {
        console.log('额外收入：', res)
        let data = res.data
        if (data.code == 200) {
          this.setData({
            todayIncomeFromShop: data.data
          })
        }
      })
  },
  /**
   * 获取停车场主信息
   */
  getParkingOwnerData(id) {
    request.http(
      '/order/orderInfo/getParkingOwnerData',
      'GET',
      { parkId: id }
    ).then(res => {
      let data = res.data
      if (data.code == 200) {
        //  如果停车场不存在，则跳转到登陆页
        if( data.data.parkId == null){
          wx.removeStorageSync('token')
          wx.removeStorageSync('parkInfo')
          wx.removeStorageSync('parkId')
          wx.reLaunch({ url: '/pages/login/login' })

          return
        }

        this.setData({
          parkingOwnerData: data.data,
          parkName: data.data.parkName,
          parkId: data.data.parkId,
          parkSource: data.data.parkSource
        })
        wx.setStorageSync("parkId",data.data.parkId)
        wx.setStorageSync('parkInfo', { avatar: data.data.avatar, mobilePhone: data.data.mobilePhone, nickname: data.data.nickname, realName: data.data.realName, parkId: data.data.parkId, isOwner: data.data.isOwner, parkSource: data.data.parkSource, reserveStatus: data.data.reserveStatus })
        this.getToadyMoney(data.data.parkId)  //  今日收益
        this.getParkTodayExtraRevenue() //  红利收入
        this.getTodoCount() //  待办事项提示点
        this.getReserveCount()  //  待处理预约申请数量
        // this.getTotalRevenue()
        // this.getTotalParking()
        this.getTodayParking()  //  今日泊车数
        this.getTotalMonthCar() //  月租车数
      }
    })
  },
  /**
   * 获取管理员停车场信息
   */
  getManagerParkInfo() {
    request.http(
      '/order/orderInfo/getParkInfo', 
      'GET'
    ).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({ parkList: data.data })
      }
    })
  },
  /**
   * 更换停车场
   */
  changeParking() {
    this.setData({
      changeParkingDialog: true
    })
  },
  /**
   * 关闭切换停车场弹窗
   */
  hiddenChangeParkingDialog() {
    this.setData({
      changeParkingDialog: false
    })
  },
  /**
   * 切换停车场
   */
  selectParking(e) {
    this.getParkingOwnerData(e.currentTarget.dataset.id)
    this.hiddenChangeParkingDialog()
  },
  /**
   * 待办事项提示点
   */
  getTodoCount() {
    let that = this
    request.http('/order/orderInfo/getTodoCount', 'Get', { parkId: this.data.parkId })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            'parkingOwnerData.todoCount': data.data
          })
        }
      })
  },
  /**
   * 待处理预约申请数量
   */
  getReserveCount() {
    request.http('/order/orderInfo/getReserveCount', 'Get', { parkId: this.data.parkId })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          this.setData({
            'parkingOwnerData.reserveCount': data.data
          })
        }
      })
  },
  /**
   * 预约页面跳转
   */
  toApply() {
    let parkInfo = wx.getStorageSync('parkInfo')
    let isOpen = parkInfo.reserveStatus
    if (isOpen == 1) {
      wx.navigateTo({
        url: '/pages/apply/apply',
      })
    } else {
      // wx.showModal({
      //   title: '操作确认',
      //   content: '当前车场预约功能未开启，是否开启车位预约功能？',
      //   cancelText: '暂不开启',
      //   confirmText: '开启预约',
      //   success(res) {
      //     if (res.confirm) {
      //       let parmas = {
      //         parkId: parkInfo.parkId,
      //         status: 1
      //       }
      //       request.http('/promotion/reserve-rule/updateReserveStatus', 'POST', parmas)
      //         .then(res => {
      //           let data = res.data
      //           if (data.code == 200) {
      //             parkInfo.reserveStatus = 1
      //             wx.setStorageSync('parkInfo', parkInfo)
      //             wx.navigateTo({
      //               url: '/pages/apply/apply?firstOpen=true',
      //             })
      //           }
      //         })
      //     }
      //   }
      // })
    }
  },
  /**
   * 获取通道码
   */
  openHandrailDialog() {
    let that = this
    request.http('/park/park-entrance/getParkChannels', 'POST', { parkId: this.data.parkId, parkSource: this.data.parkSource })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          if (data.data.status == 1) {
            that.setData({
              aisleList: data.data.channels,
              handrailDialog: true
            })
          } else {
            wx.showToast({
              title: data.data.msg,
              icon: 'none'
            })
          }
        }
      })
  },
  /**
   * 关闭抬杆弹窗
   */
  closeHandrailDialog() {
    this.setData({
      handrailDialog: false,
      selectAisleData: {}
    })
  },
  /**
   * 选择通道
   */
  selectAisle(e) {
    let index = e.currentTarget.dataset.index
    let aisleList = this.data.aisleList
    this.setData({
      selectAisleData: aisleList[index]
    })
  },
  /**
   * 抬杆
   */
  openHandrail(e) {
    let that = this
    let type = e.currentTarget.dataset.type
    let selectAisleData = this.data.selectAisleData
    if (!selectAisleData.channelId) {
      wx.showToast({ title: '请选择操作通道', icon: 'none' })

      return
    }
    wx.showModal({
      title: '操作确认',
      content: '是否确认' + (type == 1 ? '抬杆放行？' : '落杆'),
      confirmText: '确认' + (type == 1 ? '抬杆' : '落杆'),
      success(res) {
        if (res.confirm) {
          let parmas = {
            channelId: selectAisleData.channelId,
            channelIdStatus: type,
            parkId: that.data.parkId,
            parkSource: that.data.parkSource
          }
          request.http('/park/park-entrance/parkGateOnOff', 'POST', parmas)
            .then(res => {
              let data = res.data
              if (data.code == 200) {
                if (data.data.status == 1) {
                  wx.showToast({
                    title: '操作成功',
                  })
                } else {
                  wx.showToast({
                    title: data.data.msg,
                    icon: 'none'
                  })
                }
                that.closeHandrailDialog()
              }
            })
        }
      }
    })
  },
  surveillance(){
    wx.showToast({
      title: '开发中',
      icon: 'none'
    })
  },
  /**
   * 累计收入
   */
  // getTotalRevenue() {
  //   let that = this
  //   request.http('/order/orderInfo/getTotalRevenue', 'GET', { parkId: this.data.parkId })
  //     .then(res => {
  //       let data = res.data
  //       if (data.code == 200) {
  //         that.setData({
  //           'parkingOwnerData.totalRevenue': data.data
  //         })
  //       }
  //     })
  // },
  /**
   * 清零累计收入
   */
  // clearIncome() {
  //   wx.showModal({
  //     title: '操作确认',
  //     content: '是否确认数据清零？清零后将重新开始数据累计',
  //     confirmText: '确认清零',
  //     success(res) {
  //       if (res.confirm) {
  //         console.log('清零');
  //       }
  //     }
  //   })
  // },
  /**
   * 累计泊车数
   */
  // getTotalParking() {
  //   let that = this
  //   request.http('/order/orderInfo/getTotalParking', 'GET', { parkId: this.data.parkId })
  //     .then(res => {
  //       let data = res.data
  //       if (data.code == 200) {
  //         that.setData({
  //           'parkingOwnerData.totalParking': data.data
  //         })
  //       }
  //     })
  // },
  /**
   * 清零累计泊车
   */
  // clearStop() {
  //   wx.showModal({
  //     title: '操作确认',
  //     content: '是否确认数据清零？清零后将重新开始数据累计',
  //     confirmText: '确认清零',
  //     success(res) {
  //       if (res.confirm) {
  //         console.log('清零');
  //       }
  //     }
  //   })
  // },
  /**
   * 今日泊车数
   */
  getTodayParking() {
    request.http(
      '/order/orderInfo/getTodayParking', 
      'GET', 
      { parkId: this.data.parkId }
    ).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({
          'parkingOwnerData.todayParking': data.data
        })
      }
    })
  },
  /**
  * 月租车数
  */
  getTotalMonthCar() {
    request.http(
      '/customer/card/getTotalMonthCar', 
      'GET', 
      { parkId: this.data.parkId }
    ).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({
          'parkingOwnerData.totalMonthCar': data.data
        })
      }
    })
  },
})
